var userId;
var merchantId;
var result;
const defHeaders = {
  'Content-Type': 'application/json;charset=UTF-8',
  'Accept': 'application/json',
};
App({
  showOrHidden: true,
  onLaunch: function(options) {
    console.log(options)
    wx.setStorage({
      key: 'userId',
      data: {
        // 初始化
        userId: '',
      },
      success: function () {
      }
    });
    // 第一次打开
    const util = require("/utils/util.js")
    var option = options.query;
    if (JSON.stringify(option) == "{}") {
      var that = this;
        //默认门店id
        wx.setStorage({
            key: 'storeId',
            data: {
                storeId: '296731217806573568',
            },//284747337142706176 线上测试        296731217806573568 测试   
            success: function (res) {
                // console.log(res);
            },
            fail: function (log) {
                console.log(log);
            },
            complete: function (com) {
                console.log(com);
            }
        })


        //默认商户id
        wx.setStorage({
            key: "merchantId",
            data: {
                merchantId: '296730018952855552', //284744620701720576 线上测试      296730018952855552 测试 
            },
            success: function (res) {
                console.log(res);
            },
            fail: function (log) {
                console.log(log);
            },
            complete: function (com) {
                console.log(com);
            }
        })
        // 测试加人useId：2088022710203674
        // wx.setStorage({
        //   key: 'userId',
        //   data: {
        //     userId: "2088022710203674",
        //   },

        //   success: function () {
        //   }
        // });


        //默认qrcode
        wx.setStorage({
            key: "qrcodeId",
            data: {
                qrcodeId: '296732354194522112', //290708152568061952 线上测试         296732354194522112//测试 
            },
            success: function (res) {
                console.log(res);
            },
            fail: function (log) {
                console.log(log);
            },
            complete: function (com) {
                console.log(com);
            }
        })
    }


    //获取到参数
    var option = options.query;
    if (JSON.stringify(option) != "{}") {
      var arr=[];
      let str = decodeURIComponent(options.query.q)
      var name; var value;
        var num = str.indexOf("?")
        str = str.substr(num + 1); //取得所有参数   stringvar.substr(start [, length ]
        arr = str.split("&"); //各个参数放到数组里
        for (var i = 0; i < arr.length; i++) {
          num = arr[i].indexOf("=");
          if (num > 0) {
            name = arr[i].substring(0, num);
            value = arr[i].substr(num + 1);
            this[name] = value;
          }
        }
      var merchantId = arr[0].substr(arr[0].indexOf("=")+1);
      var storeId = arr[1].substr(arr[1].indexOf("=") + 1)
      var qrcodeId = arr[2].substr(arr[2].indexOf("=") + 1)
      wx.setStorage({
        key: 'merchantId',
        data: {
          merchantId: merchantId,
        },

        success: function () {
        }

      });
      wx.setStorage({
        key: 'storeId',
        data: {
          storeId: storeId,
        },

        success: function () {
        }
      });

      wx.setStorage({
        key: 'qrcodeId',
        data: {
          qrcodeId: qrcodeId,
        },

        success: function () {
        }
      });
      try {
        wx.getStorage({
          key: 'qrcodeId',
          success: function (res) {
            let qrcodeId = qrcodeId;
          },
          fail: function (res) {
            // wx.showModal({ title: res.errorMessage });
          }
        });
      } catch (e) {
        if (qrcodeId == '' || typeof (qrcodeId) == "undefined") {
          qrcodeId = ''
        }
      }

      try {
        wx.getStorage({
          key: 'merchantId',
          success: function (res) {
            let merchantId = merchantId;
          },
          fail: function (res) {
            // wx.showModal({ title: res.errorMessage });
          }
        });
      } catch (e) {
        if (qrcodeId == '' || typeof (qrcodeId) == "undefined") {
          qrcodeId = ''
        }
      }

      try {
        wx.getStorage({
          key: 'storeId',
          success: function (res) {
            let storeId = storeId;
          },
          fail: function (res) {
            // wx.showModal({ title: res.errorMessage });
          }
        });
      } catch (e) {

        if (qrcodeId == '' || typeof (qrcodeId) == "undefined") {
          qrcodeId = ''
        }
      }
    }
  },
  globalData: {
      url: 'https://devpay.vikpay.com',  //线上 https://micropay.vikpay.com 后改http://app.vikpay.com         //测试 https://devpay.vikpay.com
  },
  onShow(options) { 
    //获取到参数
    var option = options.query;
    if (JSON.stringify(option) != "{}") {
      var arr = [];
      let str = decodeURIComponent(options.query.q)
      var name; var value;
      var num = str.indexOf("?")
      str = str.substr(num + 1); //取得所有参数   stringvar.substr(start [, length ]
      arr = str.split("&"); //各个参数放到数组里
      for (var i = 0; i < arr.length; i++) {
        num = arr[i].indexOf("=");
        if (num > 0) {
          name = arr[i].substring(0, num);
          value = arr[i].substr(num + 1);
          this[name] = value;
        }
      }
      var merchantId = arr[0].substr(arr[0].indexOf("=") + 1);
      var storeId = arr[1].substr(arr[1].indexOf("=") + 1)
      var qrcodeId = arr[2].substr(arr[2].indexOf("=") + 1)
      wx.setStorage({
        key: 'merchantId',
        data: {
          merchantId: merchantId,
        },

        success: function () {
        }

      });
      wx.setStorage({
        key: 'storeId',
        data: {
          storeId: storeId,
        },

        success: function () {
        }
      });

      wx.setStorage({
        key: 'qrcodeId',
        data: {
          qrcodeId: qrcodeId,
        },

        success: function () {
        }
      });
      try {
        wx.getStorage({
          key: 'qrcodeId',
          success: function (res) {
            let qrcodeId = qrcodeId;
          },
          fail: function (res) {
            // wx.showModal({ title: res.errorMessage });
          }
        });
      } catch (e) {
        if (qrcodeId == '' || typeof (qrcodeId) == "undefined") {
          qrcodeId = ''
        }
      }

      try {
        wx.getStorage({
          key: 'merchantId',
          success: function (res) {
            let merchantId = merchantId;
          },
          fail: function (res) {
            // wx.showModal({ title: res.errorMessage });
          }
        });
      } catch (e) {
        if (qrcodeId == '' || typeof (qrcodeId) == "undefined") {
          qrcodeId = ''
        }
      }

      try {
        wx.getStorage({
          key: 'storeId',
          success: function (res) {
            let storeId = storeId;
          },
          fail: function (res) {
            // wx.showModal({ title: res.errorMessage });
          }
        });
      } catch (e) {

        if (qrcodeId == '' || typeof (qrcodeId) == "undefined") {
          qrcodeId = ''
        }
      }
        wx.setStorage({
            key: 'type',
            data: {
                type: 2,
            },
        })
      } else {
          wx.getStorage({
              key: 'type',
              success: function (res) {
                  console.log(res.data.type)
                  if (res.data.type == 3) {
                      wx.redirectTo({
                          url: '../recommend/recommend',
                      })
                  }
              }
          })
      }   


  },
    onHide() {
        wx.setStorage({
            key: 'type',
            data: {
                type: 3,
            },
        })
    }

});
